package com.qfedu.dao.impl;

import com.qfedu.dao.ProductDao;
import com.qfedu.entity.Product;
import com.qfedu.utils.BaseDao;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class ProductDaoImpl extends BaseDao implements ProductDao {
    @Override
    public List<Product> getProductByTypeAndPage(int tid, int currentPage, int pageSize) throws SQLException {
        String sql = "select p_id as pid,t_id as tid,p_name as pname,p_time as ptime ," +
                "p_image as pimage,p_state as pstate ,p_info as pinfo ,p_price as pprice " +
                "from product where t_id = ? limit ?,?";
        List<Product> productList = queryBeanList(sql, Product.class, tid, (currentPage - 1) * pageSize, pageSize);
        return productList;
    }

    @Override
    public int getProductByTypeCount(int tid) throws SQLException {
        String sql = "select count(1) from product where t_id = ?";
        long count = queryRunner.query(sql, new ScalarHandler<>(), tid);
        return (int)count;
    }

    @Override
    public Product getProductById(int pid) throws SQLException {
        String sql = "select p_id as pid,t_id as tid,p_name as pname,p_time as ptime ," +
                "p_image as pimage,p_state as pstate ,p_info as pinfo ,p_price as pprice " +
                "from product where p_id = ?";
        Product product = queryBean(sql, Product.class, pid);
        return product;
    }
}
